import { LAYER_CHANGE_TYPE } from "../common/GameConfig";
import LayerStatic from "../common/LayerStatic";
import { SoundManager, SoundName } from "../common/SoundManager";
import EventManager from "../core/event/EventManager";
import { Event_Name } from "../core/event/event_mgr";
import SDKHelper from "../core/sdk/SDKHelper";
import SidebarManager from "./SidebarManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Welcome extends cc.Component {

    @property(cc.Node)
    startBtn: cc.Node = null;
    @property(cc.Node)
    sidebarBtn: cc.Node = null;
    @property(cc.Node)
    sidebarLayer: cc.Node = null;
    @property(cc.Node)
    Rank: cc.Node = null;

    onLoad() {
        LayerStatic.Instance.setWelcomeStatic(this);
        this.startBtn.on(cc.Node.EventType.TOUCH_END, this.onStartGame, this);
        this.sidebarLayer.active = false;
        this.Rank.active = false;

    }

    protected onEnable(): void {
        globalThis.GAME_GLOBAL.levelGrade = 1;
        //需要背景音乐，放开即可，长短音乐自行修改
        SoundManager.Instance.playMusic(SoundName.bgm_short);
        SDKHelper.Instance.ttCheckSidebar((isShow: boolean) => {
            console.log("侧边栏显示状态：" + isShow);
            this.sidebarBtn.active = isShow;
        })
    }

    protected onDisable(): void {
        SoundManager.Instance.setMusicVolume(0.2);
    }

    start() {
        this.playStartBtnAnima();
    }

    protected onDestroy(): void {
        LayerStatic.Instance.setWelcomeStatic(null);
    }

    private playStartBtnAnima() {
        cc.Tween.stopAllByTarget(this.startBtn);
        cc.tween(this.startBtn)
            .to(0.5, { scale: 1.1 })
            .to(0.5, { scale: 1 })
            .union()
            .repeatForever()
            .start()
    }

    private onStartGame() {
        // this.node.dispatchEvent(new cc.Event.EventCustom('show_Main', true));
        EventManager.Instance.emit(Event_Name.LAYER_CHANGE, LAYER_CHANGE_TYPE.WELCOME_TO_GAME);
    }

    private onSidebarTouch() {

        this.sidebarLayer.getComponent(SidebarManager).showSidebarLayer({
            success: () => {
                this.sidebarBtn.active = false;
                this.sidebarLayer.active = false;
            },
            fail: () => {

            }
        })
    }


    clickRank() {
        this.Rank.active = true;
    }
    // update (dt) {}
}
